package cn.com.xxlibrary.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cn.com.xxlibrary.dao.IImportDao;
import cn.com.xxlibrary.orm.BooksBean;
import cn.com.xxlibrary.util.UtilTools;

@Transactional
@Repository("importDao")
public class ImportDaoImpl implements IImportDao {
	
	@Autowired
	@Qualifier("sessionFactory")
	private SessionFactory sessionFactory;

	@Override
	public void importBooks(List<BooksBean> content) {
		Session session = sessionFactory.getCurrentSession();
		for (BooksBean book : content) {
			List lst = session.createQuery("FROM BooksBean WHERE bookCallNo="+UtilTools.toSqlString(book.getBookCallNo())).list();
			if (lst.size()>0){
				BooksBean b = (BooksBean) lst.get(0);
				b.setBookCount(b.getBookCount()+1);
				session.update(b);
			}else{
			session.save(book);
			}
		}
	}

}
